package com.sdau.repairmaster.UIFile.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sdau.repairmaster.R;
import com.sdau.repairmaster.UIFile.ShowDeairRepairInfo;
import com.sdau.repairmaster.logic.model.RepairInfo;


import java.util.List;


public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyViewHolder> {

    private List<RepairInfo> itemList;
    private Context context;
    private onItemClickListener listener;

    public MyRecycleViewAdapter(List list){
        this.itemList = list;
    }

    public MyRecycleViewAdapter(List list, Context context, onItemClickListener listener){
        this.itemList = list;
        this.context = context;
        this.listener = listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_history_item,parent,false));
        View view = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.layout_history_item,parent,false
        );

//        MyViewHolder holder = new MyViewHolder(view);

        final MyViewHolder holder = new MyViewHolder(view);
        holder.repairInfoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                RepairInfo repairInfo = itemList.get(position);
                //跳转到具体页面
                Intent intent = new Intent(context, ShowDeairRepairInfo.class);
                intent.putExtra("address",repairInfo.getAddress());
                intent.putExtra("type",repairInfo.getType());
                intent.putExtra("describe",repairInfo.getDescribe());
                intent.putExtra("path",repairInfo.getPath());
                intent.putExtra("flag",repairInfo.getFlag()+"");
                context.startActivity(intent);
//                Toast.makeText(view.getContext(), "you clicked view " + repairInfo.getType(),
//                        Toast.LENGTH_SHORT).show();


            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
        RepairInfo repairInfo = itemList.get(position);
        holder.repairInfo_title.setText("" +repairInfo.getId());
        holder.repairInfo_content.setText(repairInfo.getType());
        holder.repairInfo_time.setText(repairInfo.getTime());
    }

    @Override
    public int getItemCount() {

        return itemList.size();
    }


    static  class MyViewHolder extends RecyclerView.ViewHolder{
        View repairInfoView;
        TextView repairInfo_title;
        TextView repairInfo_content;
        TextView repairInfo_time;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            repairInfoView = itemView;
            repairInfo_title = (TextView) itemView.findViewById(R.id.repairInfo_title);
            repairInfo_content = (TextView) itemView.findViewById(R.id.repairInfo_content);
            repairInfo_time = (TextView) itemView.findViewById(R.id.repairInfo_time);

        }
    }

    public interface  onItemClickListener{
        void onClick(int pos);
    }
}
